package com.kbxwn.logmanager;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.kbxwn.logmanager.entity.EventMessage;
import com.kbxwn.logmanager.entity.LogEntity;
import com.kbxwn.logmanager.log.LimitQueue;
import com.kbxwn.logmanager.log.LogManager;
import com.kbxwn.logmanager.log.LogService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MainActivity extends Activity {
    @BindView(R.id.textView3)
    TextView textView;

    @BindView(R.id.scrollView)
    public ScrollView scrollView;

    private Handler handler = new Handler();

    private String packageName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        packageName = getPackageName();

        EventBus.getDefault().register(this);

        initHistoryLog();

        startLogService();
    }

    public void initHistoryLog() {
        textView.setText("");

        LimitQueue<LogEntity> logQueue = LogManager.intance().getLogQueue();
        for (LogEntity logEntity : logQueue) {
            textView.append(Html.fromHtml(logEntity.log));
            textView.append("\n");
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                scrollView.smoothScrollTo(0, 100000);
            }
        });

    }

    private void startLogService() {
        if (isServiceRunning(this, LogService.class.getName())) {
            Toast.makeText(this, "日志服务已启动", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "启动日志服务", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(this, LogService.class);
            startService(intent);
        }
    }

    @OnClick(R.id.btn_filter)
    public void clickFilter() {
        Intent intent = new Intent(this, FilterActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btn_addlog)
    public void clickTest() {
        Log.e("WEA", "click cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick cleanclick clean");
    }

    @OnClick(R.id.btn_clear)
    public void clickClean() {
        textView.setText("");
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void logFresh(EventMessage msg) {
        if (msg.checkTag("show_new_log")) {
            LogEntity logEntity = (LogEntity) msg.object;
            textView.append(Html.fromHtml(logEntity.log));
            textView.append("\n");

            handler.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.smoothScrollTo(0, 100000);
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 判断服务是否已启动
     *
     * @param mContext
     * @param className 服务的名称
     * @return
     */
    public static boolean isServiceRunning(Context mContext, String className) {
        ActivityManager activityManager;
        activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

        List<ActivityManager.RunningServiceInfo> serviceList;
        serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);

        if (serviceList.size() == 0) {
            return false;
        }

        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className)) {
                return true;
            }
        }

        return false;
    }
}
